package com.ebt.m.proposal_v2.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.constant.Dictionary;
import com.ebt.m.proposal_v2.dao.response.BusinessConfig;
import com.ebt.m.proposal_v2.interfaces.OnRxClickListener;
import com.ebt.m.proposal_v2.utils.RxUtils;
import com.ebt.m.proposal_v2.widget.base.CompatLinearLayout;

import java.util.ArrayList;

import butterknife.BindView;

public class ProposalPropertyItem extends CompatLinearLayout {

    public static final int TYPE_RELATION = 1;
    public static final int TYPE_PROFESSION = 2;
    private int type = 0;

    @BindView(R.id.key)
    TextView keyView;
    @BindView(R.id.value)
    TextView valueView;
    @BindView(R.id.root)
    RelativeLayout root;

    Drawable drawableRight;
    BusinessConfig.ProposalOtherInsured.Profession profession;

    public ProposalPropertyItem(Context context) {
        super(context);
    }

    public ProposalPropertyItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.proposalPropertyItem);
        String title = t.getString(R.styleable.proposalPropertyItem_propertyKey);
        setTitle(title);
        String value = t.getString(R.styleable.proposalPropertyItem_propertyValue);
        setValue(value);
        type = t.getInt(R.styleable.proposalPropertyItem_propertyType, 0);
    }

    private void resetDrawableRight() {
        drawableRight = getDrawable(R.drawable.ic_arrow_right);
        drawableRight.setBounds(0, 0, 20, 40);
        valueView.setCompoundDrawables(null, null, drawableRight, null);
    }

    @Override
    protected int setContentView() {
        return R.layout.proposal_v2_pick;
    }

    @Override
    protected void onBindView() {
        resetDrawableRight();
    }

    public void setTitle(String title) {
        keyView.setText(title);
    }

    public void setValue(String val) {
        valueView.setText(val);
    }

    public String getValue() {
        return valueView.getText().toString().trim();
    }

    public void addPropertyChooseListener(OnRxClickListener listener) {
        RxUtils.click(root, listener);
    }

    private BusinessConfig businessConfig;

    public void setBusinessConfig(BusinessConfig config) {
        businessConfig = config;
    }

    public BusinessConfig getBusinessConfig() {
        return businessConfig;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        switch (type) {
            case TYPE_PROFESSION:
                if (businessConfig != null && businessConfig.proposal_other_insured != null && businessConfig.proposal_other_insured.profession != null) {
                    setDefaultProfession(businessConfig.proposal_other_insured.profession);
                }
                break;
        }
    }

    private void setDefaultProfession(BusinessConfig.ProposalOtherInsured.Profession profession) {
        this.profession = profession;
        if (valueView != null) {
            String careerName = Dictionary.DictionaryUtils.getCareerNameByCode(profession.defaultValue);
            valueView.setText(careerName);
        }
    }

    public ArrayList<Integer> getExceptedDataIndex(){
        ArrayList<Integer> ret = new ArrayList<>();
        switch (type) {
            case TYPE_PROFESSION:
                if (profession != null && profession.list != null) {
                    for (int i = 0; i < 6; i++){
                        if(shouldExceptProfession(i)){
                            ret.add(i);
                        }
                    }
                }
                break;
            case TYPE_RELATION:
                //TODO 后台传的多被保人的关系字段暂时不用
                /*if (businessConfig != null
                        && businessConfig.proposal_other_insured != null
                        && businessConfig.proposal_other_insured.relation != null
                        && businessConfig.proposal_other_insured.relation.list != null) {
                    for (int i = 0; i < 7; i++){
                        if(shouldExceptRelation(i)){
                            ret.add(i + 1);
                        }
                    }
                }*/
                break;
        }
        return ret;
    }

    private boolean shouldExceptProfession(int index){
        boolean should = true;
        for (Integer item : profession.list){
            if (item != null && item == index + 1) {
                should = false;
            }
        }
        return should;
    }

    private boolean shouldExceptRelation(int index){
        boolean should = true;
        for (Integer item : businessConfig.proposal_other_insured.relation.list){
            if (item != null && item == index) {
                should = false;
            }
        }
        return should;
    }

    public int getRelation(){
        int relation = -1;
        if (type == TYPE_RELATION){
            relation = Dictionary.DictionaryUtils.getRelationCodeByName(valueView.getText().toString().trim());
        }
        return relation;
    }
}
